﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net.Mail;
using System.Configuration;
using Brjostagjof.Models;
using System.Web.Security;

namespace Brjostagjof.email
{
    public class Email
    {
        static public void  NewsArticleLogMessage( NewsArticleViewModel message )
        {   
            using (MailMessage messagem = new MailMessage())
            {
                foreach (var item in message.subscribers)
                {
                    messagem.To.Add(item.Email);
                }

                messagem.Subject = String.Format("Nýtt efni á Brjóstagjöf.is: {0}", message.newsArticle.Title);

                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Send(messagem);
            }
        }

        static public void ArticleLogMessage(ArticleViewModel message)
        {
            using (MailMessage messagem = new MailMessage())
            {
                foreach (var item in message.subscribers)
                {
                    messagem.To.Add(item.Email);
                }
                messagem.Subject = String.Format("Nýtt efni á Brjóstagjöf.is: {0}", message.article.Title);
                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Send(messagem);
            }
        }

        static public void NyrFagadili(string notandi)
        {
            using (MailMessage messagem = new MailMessage())
            {
                BrjostagjofRepository rep = new BrjostagjofRepository ();

                foreach (var item in rep.GetAllAdmins() )
                {
                    messagem.To.Add(item.Email);
                }

                MembershipUser user = Membership.GetUser(notandi);
                if (user == null)
                {
                    throw new InvalidOperationException("User [" +
                        notandi + " ] not found.");
                }

                // Do whatever u want with the unique identifier.
                Guid guid = (Guid)user.ProviderUserKey;
                

                String title;
                title = "Nýr notandi hefur beðið um að vera fagaðili";
                
                messagem.Subject = String.Format(rep.GetName (guid)+ " hefur beðið um að vera fagaðili", title);
          


                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Send(messagem);

            }
        }

        /// <summary>
        /// Send mail to selected aspnet_Membership user
        /// </summary>
        /// <param name="user">aspnet_Membership user</param>
        /// <param name="message">The body of the message</param>
        /// <param name="title">The subject of the message</param>
        static public void EmailTo( aspnet_Membership user, string message, string title)
        {
            using (MailMessage messagem = new MailMessage())
            {
                messagem.To.Add(user.Email);
                messagem.Subject = title;
                messagem.Body = message;

                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Send(messagem);
            }
        }


    }
}